{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4f70b76b",
   "metadata": {
    "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
    "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
    "execution": {
     "iopub.execute_input": "2021-10-28T10:18:49.232672Z",
     "iopub.status.busy": "2021-10-28T10:18:49.227561Z",
     "iopub.status.idle": "2021-10-28T10:19:04.502716Z",
     "shell.execute_reply": "2021-10-28T10:19:04.503216Z",
     "shell.execute_reply.started": "2021-10-28T10:08:45.550332Z"
    },
    "papermill": {
     "duration": 15.287961,
     "end_time": "2021-10-28T10:19:04.503522",
     "exception": false,
     "start_time": "2021-10-28T10:18:49.215561",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: scikit-learn in /opt/conda/lib/python3.7/site-packages (0.23.2)\r\n",
      "Collecting scikit-learn\r\n",
      "  Downloading scikit_learn-1.0.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (23.2 MB)\r\n",
      "\u001b[K     |████████████████████████████████| 23.2 MB 545 kB/s \r\n",
      "\u001b[?25hRequirement already satisfied: joblib>=0.11 in /opt/conda/lib/python3.7/site-packages (from scikit-learn) (1.0.1)\r\n",
      "Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/conda/lib/python3.7/site-packages (from scikit-learn) (2.2.0)\r\n",
      "Requirement already satisfied: numpy>=1.14.6 in /opt/conda/lib/python3.7/site-packages (from scikit-learn) (1.19.5)\r\n",
      "Requirement already satisfied: scipy>=1.1.0 in /opt/conda/lib/python3.7/site-packages (from scikit-learn) (1.7.1)\r\n",
      "Installing collected packages: scikit-learn\r\n",
      "  Attempting uninstall: scikit-learn\r\n",
      "    Found existing installation: scikit-learn 0.23.2\r\n",
      "    Uninstalling scikit-learn-0.23.2:\r\n",
      "      Successfully uninstalled scikit-learn-0.23.2\r\n",
      "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\r\n",
      "pdpbox 0.2.1 requires matplotlib==3.1.1, but you have matplotlib 3.4.3 which is incompatible.\r\n",
      "hypertools 0.7.0 requires scikit-learn!=0.22,<0.24,>=0.19.1, but you have scikit-learn 1.0.1 which is incompatible.\u001b[0m\r\n",
      "Successfully installed scikit-learn-1.0.1\r\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\r\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install scikit-learn -U"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4b69c03e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-28T10:19:04.545681Z",
     "iopub.status.busy": "2021-10-28T10:19:04.544725Z",
     "iopub.status.idle": "2021-10-28T10:19:05.501277Z",
     "shell.execute_reply": "2021-10-28T10:19:05.500590Z",
     "shell.execute_reply.started": "2021-10-28T10:09:03.182001Z"
    },
    "papermill": {
     "duration": 0.979723,
     "end_time": "2021-10-28T10:19:05.501434",
     "exception": false,
     "start_time": "2021-10-28T10:19:04.521711",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "from sklearn.datasets import make_classification\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X, y = make_classification(n_samples=300, n_features=50, \n",
    "                           n_informative=10,\n",
    "                           n_redundant=25, n_repeated=15, \n",
    "                           n_clusters_per_class=5,\n",
    "                           flip_y=0.05, class_sep=0.5, \n",
    "                           random_state=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "62513810",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-28T10:19:05.542295Z",
     "iopub.status.busy": "2021-10-28T10:19:05.541309Z",
     "iopub.status.idle": "2021-10-28T10:19:05.563842Z",
     "shell.execute_reply": "2021-10-28T10:19:05.563327Z",
     "shell.execute_reply.started": "2021-10-28T10:09:04.087478Z"
    },
    "papermill": {
     "duration": 0.045382,
     "end_time": "2021-10-28T10:19:05.563986",
     "exception": false,
     "start_time": "2021-10-28T10:19:05.518604",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "from sklearn import svm\n",
    "\n",
    "svc = svm.SVC()\n",
    "svc = svm.SVC(probability=True, random_state=1)\n",
    "\n",
    "from sklearn import model_selection\n",
    "search_grid = [\n",
    "               {'C': [1, 10, 100, 1000], 'kernel': ['linear']},\n",
    "               {'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001],\n",
    "               'kernel': ['rbf']}\n",
    "               ]\n",
    "               \n",
    "scorer = 'accuracy'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b949dbdf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-28T10:19:05.604674Z",
     "iopub.status.busy": "2021-10-28T10:19:05.603884Z",
     "iopub.status.idle": "2021-10-28T10:34:48.628686Z",
     "shell.execute_reply": "2021-10-28T10:34:48.629211Z"
    },
    "papermill": {
     "duration": 943.048701,
     "end_time": "2021-10-28T10:34:48.629569",
     "exception": false,
     "start_time": "2021-10-28T10:19:05.580868",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'C': 10, 'gamma': 0.001, 'kernel': 'rbf'}\n",
      "0.7\n"
     ]
    }
   ],
   "source": [
    "search_func = model_selection.GridSearchCV(estimator=svc, \n",
    "                                           param_grid=search_grid,\n",
    "                                           scoring=scorer, \n",
    "                                           n_jobs=-1,\n",
    "                                           cv=5)\n",
    "search_func.fit(X, y)\n",
    "\n",
    "print (search_func.best_params_)\n",
    "print (search_func.best_score_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "98d9aa9d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-28T10:34:48.670290Z",
     "iopub.status.busy": "2021-10-28T10:34:48.669534Z",
     "iopub.status.idle": "2021-10-28T10:42:04.936145Z",
     "shell.execute_reply": "2021-10-28T10:42:04.936654Z"
    },
    "papermill": {
     "duration": 436.289573,
     "end_time": "2021-10-28T10:42:04.936849",
     "exception": false,
     "start_time": "2021-10-28T10:34:48.647276",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'C': 300.1387499644088, 'gamma': 0.014479216173691752, 'kernel': 'rbf'}\n",
      "0.6666666666666667\n"
     ]
    }
   ],
   "source": [
    "import scipy.stats as stats\n",
    "from sklearn.utils.fixes import loguniform\n",
    "\n",
    "search_dict = {'kernel': ['linear', 'rbf'], \n",
    "               'C': loguniform(1, 1000),\n",
    "               'gamma': loguniform(0.0001, 0.1)\n",
    "               }\n",
    "\n",
    "scorer = 'accuracy'\n",
    "\n",
    "search_func = model_selection.RandomizedSearchCV(estimator=svc,\n",
    "                                                 param_distributions=search_dict,\n",
    "                                                 n_iter=6,\n",
    "                                                 scoring=scorer,\n",
    "                                                 n_jobs=-1,\n",
    "                                                 cv=5\n",
    "                                                 )\n",
    "\n",
    "search_func.fit(X, y)\n",
    "\n",
    "print (search_func.best_params_)\n",
    "print (search_func.best_score_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "72e2b127",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-10-28T10:42:04.981942Z",
     "iopub.status.busy": "2021-10-28T10:42:04.981239Z",
     "iopub.status.idle": "2021-10-28T10:43:10.549111Z",
     "shell.execute_reply": "2021-10-28T10:43:10.549612Z"
    },
    "papermill": {
     "duration": 65.594403,
     "end_time": "2021-10-28T10:43:10.549795",
     "exception": false,
     "start_time": "2021-10-28T10:42:04.955392",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'C': 86.63279761354555, 'gamma': 0.002054762512591133, 'kernel': 'linear'}\n",
      "0.6166666666666667\n"
     ]
    }
   ],
   "source": [
    "from sklearn.experimental import enable_halving_search_cv\n",
    "from sklearn.model_selection import HalvingRandomSearchCV\n",
    "\n",
    "search_func =  HalvingRandomSearchCV(estimator=svc,\n",
    "                                     param_distributions=search_dict,\n",
    "                                     resource='n_samples',\n",
    "                                     max_resources=100,\n",
    "                                     aggressive_elimination=True,\n",
    "                                     scoring=scorer,\n",
    "                                     n_jobs=-1,\n",
    "                                     cv=5,\n",
    "                                     random_state=0)\n",
    "\n",
    "search_func.fit(X, y)\n",
    "\n",
    "print (search_func.best_params_)\n",
    "print (search_func.best_score_)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "papermill": {
   "default_parameters": {},
   "duration": 1470.658665,
   "end_time": "2021-10-28T10:43:11.481625",
   "environment_variables": {},
   "exception": null,
   "input_path": "__notebook__.ipynb",
   "output_path": "__notebook__.ipynb",
   "parameters": {},
   "start_time": "2021-10-28T10:18:40.822960",
   "version": "2.3.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
